package com.wd.gui;

import com.wd.dominio.Inventario;
import com.wd.dominio.Item;
import com.wd.dominio.Lugar;
import com.wd.dominio.Producto;
import com.wd.dominio.Tienda;
import com.wd.gui.controlparticular.ControlGuiInventario;
import com.wd.gui.controlparticular.ControlGuiLugar;
import com.wd.gui.controlparticular.ControlGuiProducto;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gabylis
 */
public class VentanaConsultaInventarioTienda extends javax.swing.JFrame {

    private Vector<Tienda> vecTiendas;

    private Vector<Lugar> vecCiudades;

    private ControlGuiLugar controlLugar;
    
    private ControlGuiInventario controlInventario;
    
    private ControlGuiProducto controlProducto;

    private DefaultTableModel dmTeo;

    private DefaultTableModel dmCam;

    private Vector<Producto> vecProd;

    private Vector<Producto> vecProdAuxTeo;

    private Vector<Producto> vecProdAuxCam;

    private Vector<Inventario> vecTeo;

    private Vector<Item> vecItem;

    /** Creates new form VentanaConsultaInventarioTienda */
    public VentanaConsultaInventarioTienda(Vector<Tienda> result) {
        initComponents();
        java.net.URL url = getClass().getResource("Iconos/icon_016.png");
        java.awt.Image imagen = getToolkit().getImage(url);
        setIconImage (imagen);

        vecTiendas = new Vector();
        vecCiudades= new Vector();
        vecTeo = new Vector();
        vecProd = new Vector();
        vecProdAuxTeo = new Vector();
        vecProdAuxCam = new Vector();
        vecItem = new Vector();

        this.controlProducto = new ControlGuiProducto();
        vecProd = controlProducto.traerTodosLosProductos();

        vecTiendas = result;
        controlLugar = new ControlGuiLugar();
        vecCiudades = controlLugar.traerTodosLosLugares(2);

        for (Tienda tienda : result) {
            this.comboTiendas.addItem(tienda.getNombre());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        panelInventario = new javax.swing.JTabbedPane();
        panelCamino = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaTeorico = new javax.swing.JTable();
        panelTeorico = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaCamino = new javax.swing.JTable();
        comboTiendas = new javax.swing.JComboBox();
        labelTiendas = new javax.swing.JLabel();
        labelCodigoNom = new javax.swing.JLabel();
        labelTelefonoNomb = new javax.swing.JLabel();
        labelCorreoNomb = new javax.swing.JLabel();
        labelDirNomb = new javax.swing.JLabel();
        labelUbicacionNomb = new javax.swing.JLabel();
        labelCodigo = new javax.swing.JLabel();
        labelTelefono = new javax.swing.JLabel();
        labelCorreo = new javax.swing.JLabel();
        labelDir = new javax.swing.JLabel();
        labelUbicacion = new javax.swing.JLabel();
        buttonCerrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inventario de una Tienda");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Inventario por Tienda", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        panelInventario.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 102, 204), 1, true), "Inventarios Asociados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N
        panelInventario.setToolTipText("Inventario de una Tienda tanto Teórico como en camino");
        panelInventario.setFont(new java.awt.Font("Tahoma", 1, 11));

        tablaTeorico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tablaTeorico);

        javax.swing.GroupLayout panelCaminoLayout = new javax.swing.GroupLayout(panelCamino);
        panelCamino.setLayout(panelCaminoLayout);
        panelCaminoLayout.setHorizontalGroup(
            panelCaminoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCaminoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 599, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );
        panelCaminoLayout.setVerticalGroup(
            panelCaminoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCaminoLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                .addContainerGap())
        );

        panelInventario.addTab("Inventario Teórico", panelCamino);

        tablaCamino.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tablaCamino);

        javax.swing.GroupLayout panelTeoricoLayout = new javax.swing.GroupLayout(panelTeorico);
        panelTeorico.setLayout(panelTeoricoLayout);
        panelTeoricoLayout.setHorizontalGroup(
            panelTeoricoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 640, Short.MAX_VALUE)
            .addGroup(panelTeoricoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTeoricoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        panelTeoricoLayout.setVerticalGroup(
            panelTeoricoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 209, Short.MAX_VALUE)
            .addGroup(panelTeoricoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTeoricoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        panelInventario.addTab("Inventario en Camino", panelTeorico);

        comboTiendas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboTiendasActionPerformed(evt);
            }
        });

        labelTiendas.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTiendas.setText("Tiendas:");

        labelCodigoNom.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCodigoNom.setText("ID: ");

        labelTelefonoNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTelefonoNomb.setText("Teléfono: ");

        labelCorreoNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCorreoNomb.setText("Correo Electrónico:");

        labelDirNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelDirNomb.setText("Dirección:");

        labelUbicacionNomb.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelUbicacionNomb.setText("Ubicación:");

        labelCodigo.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCodigo.setForeground(new java.awt.Color(0, 102, 204));
        labelCodigo.setText("ID");

        labelTelefono.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTelefono.setForeground(new java.awt.Color(0, 102, 204));
        labelTelefono.setText("Telefono");

        labelCorreo.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCorreo.setForeground(new java.awt.Color(0, 102, 204));
        labelCorreo.setText("email");

        labelDir.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelDir.setForeground(new java.awt.Color(0, 102, 204));
        labelDir.setText("direccion");

        labelUbicacion.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelUbicacion.setForeground(new java.awt.Color(0, 102, 204));
        labelUbicacion.setText("ciudad+estado");

        buttonCerrar.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonCerrar.setText("Cerrar");
        buttonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelTiendas)
                            .addComponent(labelCodigoNom)
                            .addComponent(labelTelefonoNomb)
                            .addComponent(labelCorreoNomb)
                            .addComponent(labelDirNomb)
                            .addComponent(labelUbicacionNomb))
                        .addGap(144, 144, 144)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUbicacion)
                            .addComponent(labelDir)
                            .addComponent(labelCorreo)
                            .addComponent(labelTelefono)
                            .addComponent(labelCodigo)
                            .addComponent(comboTiendas, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelInventario, javax.swing.GroupLayout.DEFAULT_SIZE, 655, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(213, 213, 213)
                        .addComponent(buttonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboTiendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTiendas))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodigoNom)
                    .addComponent(labelCodigo))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTelefonoNomb)
                    .addComponent(labelTelefono))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCorreoNomb)
                    .addComponent(labelCorreo))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDirNomb)
                    .addComponent(labelDir))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUbicacionNomb)
                    .addComponent(labelUbicacion))
                .addGap(26, 26, 26)
                .addComponent(panelInventario, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonCerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboTiendasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboTiendasActionPerformed
        int select = -1;
        Integer codigoTienda = -1;
        int cityId = -1;
        String telefono = "";
        String direccion = "";
        String correo = "";
        String ubicacion = "";

        select = this.comboTiendas.getSelectedIndex();
        Tienda tiendita = new Tienda();
        tiendita = this.vecTiendas.elementAt(select);

        cityId = tiendita.getLUGAR_id();
        codigoTienda = tiendita.getCodigo();
        telefono = tiendita.getTelefono();
        direccion = tiendita.getDireccion();
        correo = tiendita.getCorreo();
        ubicacion = this.getUbicacion(cityId);

        this.labelCodigo.setText(codigoTienda+"");
        this.labelTelefono.setText(telefono);
        this.labelDir.setText(direccion);
        this.labelCorreo.setText(correo);
        this.labelUbicacion.setText(ubicacion);

        this.llenarTablaTeorico(codigoTienda);
        this.llenarTablaCamino(codigoTienda);
    }//GEN-LAST:event_comboTiendasActionPerformed

    private void buttonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCerrarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_buttonCerrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Tienda> result) {
                new VentanaConsultaInventarioTienda(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCerrar;
    private javax.swing.JComboBox comboTiendas;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelCodigoNom;
    private javax.swing.JLabel labelCorreo;
    private javax.swing.JLabel labelCorreoNomb;
    private javax.swing.JLabel labelDir;
    private javax.swing.JLabel labelDirNomb;
    private javax.swing.JLabel labelTelefono;
    private javax.swing.JLabel labelTelefonoNomb;
    private javax.swing.JLabel labelTiendas;
    private javax.swing.JLabel labelUbicacion;
    private javax.swing.JLabel labelUbicacionNomb;
    private javax.swing.JPanel panelCamino;
    private javax.swing.JTabbedPane panelInventario;
    private javax.swing.JPanel panelTeorico;
    private javax.swing.JTable tablaCamino;
    private javax.swing.JTable tablaTeorico;
    // End of variables declaration//GEN-END:variables

    public String getUbicacion(int cityId){
        String nombreCity= "";
        String estado = "";
        String ubicacion = "";

        for (Lugar ciudad : vecCiudades) {
            if (ciudad.getId() == cityId){
                nombreCity = ciudad.getNombrePropio();
                estado = ciudad.getNombrePertenece();
                ubicacion = nombreCity + ", " + estado;
            }
        }
        return ubicacion;
    }

    public void llenarTablaTeorico(Integer codigoTienda){
        dmTeo = new DefaultTableModel();
        dmTeo.addColumn("ID Poducto");
        dmTeo.addColumn("Nombre Producto");
        dmTeo.addColumn("Descripcion");
        dmTeo.addColumn("Cantidad");

        this.controlInventario = new ControlGuiInventario();
        this.vecTeo = controlInventario.traerInventarioTeoTienda(codigoTienda);

        this.llenarVecProdAuxTeo();

        Inventario inv = new Inventario();
        Producto prod = new Producto();
        for (int i = 0; i < vecTeo.size(); i++) {
            inv = vecTeo.elementAt(i);
            prod = this.vecProdAuxTeo.elementAt(i);

            Vector info = new Vector();
            info.addElement(prod.getId());
            info.addElement(prod.getNombre());
            info.addElement(prod.getDescripcion());
            info.addElement(inv.getCantidad());
            dmTeo.addRow(info);
        }
        this.tablaTeorico.setModel(dmTeo);
    }

    public void llenarTablaCamino(Integer codigoTienda){
        dmCam = new DefaultTableModel();
        dmCam.addColumn("ID Poducto");
        dmCam.addColumn("Nombre Producto");
        dmCam.addColumn("Descripcion");
        dmCam.addColumn("Cantidad");
        dmCam.addColumn("Id Solicitud");

        this.controlInventario = new ControlGuiInventario();
        this.vecItem = controlInventario.traerInventarioCaminoTienda(codigoTienda);

        this.llenarVecProdAuxCam();

        Item it = new Item();
        Producto prod = new Producto();

        for (int i = 0; i < vecItem.size(); i++) {
            it = vecItem.elementAt(i);
            prod = this.vecProdAuxCam.elementAt(i);

            Vector info = new Vector();
            info.addElement(prod.getId());
            info.addElement(prod.getNombre());
            info.addElement(prod.getDescripcion());
            info.addElement(it.getCantidad());
            info.addElement(it.getIdSolicitud());
            dmCam.addRow(info);
        }
        this.tablaCamino.setModel(dmCam);

    }

    public void llenarVecProdAuxTeo(){
        this.vecProdAuxTeo.removeAllElements();

        for (Inventario inv : vecTeo) {
            for (Producto prod : vecProd) {
                if(inv.getProductoId() == prod.getId()){
                this.vecProdAuxTeo.addElement(prod);
                }
            }

        }
    }

    public void llenarVecProdAuxCam(){
        this.vecProdAuxCam.removeAllElements();

        for (Item inv : vecItem) {
            for (Producto prod : vecProd) {
                if(inv.getIdProducto() == prod.getId()){
                    this.vecProdAuxCam.addElement(prod);
                }
            }
        }
    }
}
